package cn.bytecollege.user.dao;

import cn.bytecollege.common.dao.BaseDao;
import cn.bytecollege.common.entity.Cart;

import java.util.List;

public class CartDao extends BaseDao<Cart,Integer> {

    /**
     * 查询商品名，数量，价格
     *
     * @return
     * @author zhy
     */

    @Override
    public List<Cart> find(Object... objects) {
        String sql = "SELECT C1.CART_ID,C1.CUISINE_ID,CUISINE_NAME,NUMBER,CUISINE_PRICE FROM CART C1 INNER JOIN CUISINE C2 ON C1.CUISINE_ID = C2.CUISINE_ID";
        return helper.executeQuery(sql,Cart.class);
    }

    /**
     * 根据购物车ID查询菜品数量
     * @author zhy
     * @param cartId
     * @return
     */
    public int findByCartId(Integer cartId){
        String sql = "SELECT NUMBER FROM CART WHERE CART_ID = ?";
        return helper.executeScale(sql,cartId);
    }


    /**
     * 根据ID修改菜品数量
     * @param cart
     * @return
     */
    public int updateByCartId(Cart cart){
        String sql = "UPDATE CART SET NUMBER = ? WHERE CART_ID = ?";
        Object[] objects = {cart.getNumber(),cart.getCartId()};
        return helper.executeUpdate(sql,objects);
    }
    /**
     * 根据ID删除购物车中数据
     * @author zhy
     * @param cartId
     * @return
     */
    @Override
    public int deleteById(Integer cartId) {
        String sql = "DELETE FROM CART WHERE CART_ID = ?";
        return helper.executeUpdate(sql,cartId);
    }

    /**
     * 清空购物车
     * @return
     */
    public int clearCart(){
        String sql = "DELETE FROM CART";
        return helper.executeUpdate(sql);
    }

    /**
     * 根据菜品id查询购物车id
     * @author zsm
     * @param cuisineId
     * @return
     */
    public int findCartIdByCusineId(Integer cuisineId){
        String sql = "SELECT CART_ID FROM CART WHERE CUISINE_ID = ?";
        return helper.executeScale(sql,cuisineId);
    }
    /**
     * 添加到购物车
     * @param cart
     * @return
     * Author：石晓琳
     */
    @Override
    public int save(Cart cart) {
        String sql = "INSERT INTO CART (cuisine_id,NUMBER) VALUES(?,1)";
        return helper.executeUpdate(sql,cart.getCuisineId());

    }

    /**
     * 根据菜品ID查找菜品数量
     * @param cuisineId
     * @return
     */
    public int findByCuisineId(Integer cuisineId) {
        String sql = "SELECT NUMBER FROM CART WHERE CUISINE_ID = ?";
        return helper.executeScale(sql,cuisineId);
    }

    /**
     * 根据菜品ID修改菜品数量
     * @param cart
     * @return
     */
    @Override
    public int update(Cart cart) {
        String sql = "UPDATE CART SET NUMBER = ? WHERE CUISINE_ID = ?";
        Object[] objects = {cart.getNumber(),cart.getCuisineId()};
        return helper.executeUpdate(sql,objects);
    }
}
